#include "D3DStreamOutDbg.h"
#include "D3DVoxelChunk.h"
#include <stdio.h>
#include "D3DShaderCache.h"
D3DStreamOutDbg::D3DStreamOutDbg(void)
{

}
void D3DStreamOutDbg::InitStreamOutDbg(ID3D11Device*  pDevice, ID3D11DeviceContext*		pImmediateContext)
{
	m_pDevice = pDevice;
	m_pImmediateContext = pImmediateContext;
	m_ppBuffers = new ID3D11Buffer*[MAX_STREAMOUTDBG_BUFFER];
	for(int i = 0; i < MAX_STREAMOUTDBG_BUFFER; i++ )
	{
		m_ppBuffers[i] = NULL;

	}
	m_iCurrentIndex = 0;
	m_pBufferAddress = new vector<unsigned int>();
}
void D3DStreamOutDbg::DebugTexture(ID3D11Texture3D*    pTex)
{

	ID3D11Texture3D *dbgTex = NULL;
	HRESULT hr = S_OK;
	D3D11_TEXTURE3D_DESC dbgDesc;
	pTex->GetDesc(&dbgDesc);

	dbgDesc.BindFlags = 0;
	dbgDesc.CPUAccessFlags = D3D10_CPU_ACCESS_READ;
	dbgDesc.Usage = D3D11_USAGE_STAGING;

	hr = m_pDevice->CreateTexture3D(&dbgDesc, NULL, &dbgTex);

	if( FAILED( hr ) )
    {
        MessageBox( NULL,
			L"CreateBuffer : Debug ID3D11Texture3D", L"Error", MB_OK );

    }

	
	m_pImmediateContext->CopyResource(dbgTex, pTex);
	D3D11_MAPPED_SUBRESOURCE ms;
	if(m_pImmediateContext->Map(dbgTex, 0, D3D11_MAP_READ, 0, &ms) == S_OK)
	{

		struct SOStruct3
		{
			/*UINT inteOne_;
			UINT inteTwo_;
			UINT inteThree_;*/
			UINT inte_;
		};

		SOStruct3 *sodata3 = (SOStruct3 *)ms.pData;


		for(int i = 0; i <  D3DVoxelConstants::VoxelsPerChunkAxisCube; i++ )
		{
			if(sodata3[i].inte_ > 0 )
			{
				
				int kk = 0;
			}
		}

		int xDim = 128;//GetSmallest4Multiple( D3DVoxelConstants::VoxelsPerChunkAxis * 3);
		for(int z = 0; z <  D3DVoxelConstants::VoxelsPerChunkAxis; z++ )
			for(int y = 0; y <  D3DVoxelConstants::VoxelsPerChunkAxis; y++ )
				for(int x = 0; x <  xDim; x++ )
				{
					int i = x + y * xDim + z * xDim *  D3DVoxelConstants::VoxelsPerChunkAxis;
					if(sodata3[i].inte_ > 0 )
					{
						
						int kk = 0;
					}
				}
		m_pImmediateContext->Unmap(dbgTex, NULL);   
		
	}
	dbgTex->Release();

}
void D3DStreamOutDbg::DebugBuffer(ID3D11Buffer*    pBuffer)
{
	ID3D11Buffer *dbgBuffer = NULL;
	HRESULT hr = S_OK;
	D3D11_BUFFER_DESC dbgDesc;

	pBuffer->GetDesc(&dbgDesc);
	dbgDesc.Usage = D3D11_USAGE_STAGING;
	dbgDesc.BindFlags = 0;
	dbgDesc.CPUAccessFlags = D3D10_CPU_ACCESS_READ;

	hr = m_pDevice->CreateBuffer( &dbgDesc, NULL, &dbgBuffer );
	
	if( FAILED( hr ) )
    {
        MessageBox( NULL,
			L"CreateBuffer : Debug Buffer", L"Error", MB_OK );

    }

	
	m_pImmediateContext->CopyResource(dbgBuffer, pBuffer);
	D3D11_MAPPED_SUBRESOURCE ms;
	if(m_pImmediateContext->Map(dbgBuffer, 0, D3D11_MAP_READ, 0, &ms) == S_OK)
	{
		struct SOStruct
		{

			float pos[4];
			float normal[3];
			float color[4];
			float pd;
		};
		struct SOStruct2
		{

			UINT inte;
			UINT pd[3];
		};
		struct SOStruct3
		{

			UINT inte;

		};
		SOStruct *sodata = (SOStruct *)ms.pData;
		int k = sizeof(SOStruct);
		SOStruct2 *sodata2 = (SOStruct2 *)ms.pData;
		k = sizeof(SOStruct2);

		SOStruct3 *sodata3 = (SOStruct3 *)ms.pData;
		k = sizeof(SOStruct3);

		static int numInvocations = 0;
		if( numInvocations == 3 )
		{
			int lkbajdlbjht = 0;
		}
		numInvocations++;

		for(int i = 0; i <  D3DVoxelConstants::VoxelsPerChunkAxisCube; i++ )
		{
			if(sodata3[i].inte > 0 )
			{
				int _case = sodata3[i].inte & 255;

				int x = (sodata3[i].inte >> 8) & 255;
				int y = (sodata3[i].inte >> 16) & 255;
				int z = (sodata3[i].inte >> 24) & 255;
				int kk = 0;
			}
		}
		for(int i = 0; i <  D3DVoxelConstants::VoxelsPerChunkAxisCube; i++ )
		{

				int kk = 0;
		}

		/*if( true )
		{
			FILE *fp;
			errno_t err = fopen_s(&fp, "voxelmarkers.bin", "wb+");
			fwrite( sodata,  D3DVoxelConstants::VoxelsPerChunkAxisCube, 1, fp);
			fclose(fp);
		}*/


		m_pImmediateContext->Unmap(dbgBuffer, NULL);   
		
	}
	dbgBuffer->Release();
}